package com.zsc.yygh.user.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.yygh.common.exception.YyghException;
import com.zsc.yygh.common.result.Result;
import com.zsc.yygh.model.acl.User;
import com.zsc.yygh.model.user.UserBack;
import com.zsc.yygh.user.service.UserBackService;
import com.zsc.yygh.vo.user.QueryInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

//后台用户管理接口
@RestController
@RequestMapping("/api/user/userBack")
@Api(tags = "后台用户管理接口")
public class UserBackApiController {


    @Autowired
    private UserBackService userBackService;

    @PostMapping("/listUserBack")
    public Result listUserBack(@RequestBody QueryInfo queryInfo){
        QueryWrapper<UserBack> userBackQueryWrapper = new QueryWrapper<>();
        String username = queryInfo.getUsername();
        String email = queryInfo.getEmail();
        String phone = queryInfo.getPhone();
        Long pageNum = queryInfo.getPageNum();
        Long pageSize = queryInfo.getPageSize();


        if(username !=null && !username.isEmpty()){
            userBackQueryWrapper.like("username",username);
        }
        if(email !=null && !email.isEmpty()){
            userBackQueryWrapper.like("email",email);
        }
        if(phone !=null && !phone.isEmpty()){
            userBackQueryWrapper.eq("phone",phone);
        }

        //Page<UserBack> userBackPage = new Page<>(Long.parseLong(pageNum),Long.parseLong(pageSize));
        Page<UserBack> userBackPage = new Page<>(pageNum,pageSize);
        Page<UserBack> userBackList = userBackService.page(userBackPage, userBackQueryWrapper);

        return Result.ok(userBackList);
    }


    @GetMapping("/getUserOne/{id}")
    public Result getUserOne(@PathVariable Long id){
        UserBack userBack = userBackService.getById(id);
        userBack.setPassword(null);
        return Result.ok(userBack);
    }

    @PostMapping("/updateUser")
    public Result updateUserById(@RequestBody UserBack userBack){
        boolean flag = userBackService.updateById(userBack);
        return Result.ok(flag);
    }

    @PostMapping("/deleteUser")
    public Result deleteUserById(@RequestBody Long[] ids){
        userBackService.removeByIds(Arrays.asList(ids));
        return Result.ok();
    }

    @PostMapping("/saveUserBack")
    public Result saveUserBack(@RequestBody UserBack userBack){
        boolean flag = userBackService.save(userBack);
        return Result.ok(flag);
    }

    @GetMapping("/checkUser")
    public Result checkUsernameAndPassword(@RequestParam String username,
                                           @RequestParam String password){
        QueryWrapper<UserBack> userBackQueryWrapper = new QueryWrapper<>();
        userBackQueryWrapper.eq("username",username);
        UserBack userBack = userBackService.getOne(userBackQueryWrapper);
        if (userBack == null){
            return Result.fail();
        }
        if(!userBack.getPassword().equals(password)){
            return Result.fail();
        }
        return Result.ok(true);
    }

}
